﻿using AutoMapper;
using J.Model;
using J.ProductSet.Application.Command.ProductCommand;
using MediatR;
using Minio.DataModel;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace J.ProductSet.Application.CommandHandler.ProductCommandHandler
{
    public class ProductUnmountCommandHandler : IRequestHandler<ProductUnmountCommand, int>
    {
        private readonly ISqlSugarClient _sqlSugarClient;
        public IMapper _mapper;
        public ProductUnmountCommandHandler(ISqlSugarClient sqlSugarClient, IMapper mapper)
        {
            _sqlSugarClient = sqlSugarClient;
            _mapper = mapper;
        }

        public async Task<int> Handle(ProductUnmountCommand request, CancellationToken cancellationToken)
        {
            var id = request.ProductID.Split(",");
            var statusToBeUpdated = request.CommodityStatus; // 假设要更新的状态值

            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Server=10.31.56.25:5236; User Id=ZJS; PWD=123456789;DATABASE=DAMENG",
                DbType = DbType.Dm, // 数据库类型，这里以SqlServer为例
                IsAutoCloseConnection = true, // 自动关闭数据库连接
            });

            if (statusToBeUpdated == true)
            {
                statusToBeUpdated = false;
            }
            else if (statusToBeUpdated == false)
            {
                statusToBeUpdated = true;
            }

            var updateResult = await db.Updateable<Product>()
                .SetColumns(it => new Product() { CommodityStatus = statusToBeUpdated })
                .Where(it => id.Contains(it.ProductID.ToString()))
                .ExecuteCommandAsync();

            return updateResult;
        }
    }
}
